#include <algorithm>
using namespace std;
class Solution {
public:
    int jump(int A[], int n) {
        for (int ans=0,i=0,j=0,nextj=0;i<=nextj && i<n;i++) {
            if (n-1<=j) return ans;
            nextj=max(nextj, i+A[i]);
            if (i==j) {
                j=nextj;
                ++ans;
            }
        }
        return -1;
    }
};
